package fpzhan.plane.program.connect;

import fpzhan.plane.program.constant.ComposeType;
import fpzhan.plane.program.describe.CheckParamForLose;
import fpzhan.plane.program.describe.CodeBlockComment;
import fpzhan.plane.program.function.CodeBlockFunction;

import java.util.Collections;

public class TryCatchMixCodeBlockConnect extends MixCodeBlockConnect {



    public TryCatchMixCodeBlockConnect(CodeBlockContext context, ChainCodeBlockConnect source) {
        super(context, source);
        this.aloneCodeBlockCompose = new TryCatchCodeBlockCompose();
    }


    public CodeBlockComment<TryCatchCodeBlockCompose,ChainCodeBlockConnect> trys() {
        ChainCodeBlockConnect connect = new ChainCodeBlockConnect(getContext(),
                this);
        ((TryCatchCodeBlockCompose) this.aloneCodeBlockCompose).setChild(connect);
        return new CodeBlockComment(((TryCatchCodeBlockCompose) this.aloneCodeBlockCompose), connect);
    }

    public <T extends Exception> CheckParamForLose<TryCatchCodeBlockCompose,TryCatchMixCodeBlockConnect>  catchs(Class<T> exception,CodeBlockFunction catchFunction){
        ChainCodeBlockCompose compose = new ChainCodeBlockCompose(catchFunction, ComposeType.CATCH);
        ((TryCatchCodeBlockCompose) this.aloneCodeBlockCompose).addCatchs(exception,compose);
        return new CheckParamForLose(compose, this);
    }

    public ChainCodeBlockConnect finallys(){
        return finallys(null).none().comment(null);
    }

    public CheckParamForLose<TryCatchCodeBlockCompose,ChainCodeBlockConnect> finallys(CodeBlockFunction finallyFunction) {
        ChainCodeBlockCompose compose = new ChainCodeBlockCompose(finallyFunction, ComposeType.FINALLY);
        ((TryCatchCodeBlockCompose) this.aloneCodeBlockCompose).setFinallys(compose);
        ((ChainCodeBlockConnect)getSource()).addChainBlocks(Collections.singletonList(this.aloneCodeBlockCompose));
        return new CheckParamForLose(compose, (ChainCodeBlockConnect)getSource());
    }


}
